package com.fengsh.nacos.config;

import com.fengsh.nacos.components.PartsCustomGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @标题 全局路由配置API方式
 * @作者 fengsh998
 * @创建时间 2024/1/31
 * @功能描述
 */

@Configuration
public class GlobalGatewayRouteConfig {
    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        System.out.println("全局路由处理!");
        return builder.routes()
                .route("application1_route",r -> r.path("/app1/**").filters(f -> f.stripPrefix(1)).uri("lb://application1"))
                .build();
    }

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder, PartsCustomGatewayFilterFactory filterFactory) {
        System.out.println("路由带局部过滤器-----!");
        return builder.routes()
                .route("application1_route",r -> r.path("/app1/**")
                        .filters(f -> f.stripPrefix(1).filter(filterFactory.apply(new PartsCustomGatewayFilterFactory.Config("aa1",20,true))))
                        .uri("lb://application1"))
                .build();
    }
}
